<template>
  <div class="home-box">
    <!-- 顶部导航 -->
    <van-nav-bar fixed class="page-nav-bar">
      <van-button to="/search" class="search-btn" slot="title" icon="search" type="primary">搜索</van-button>
    </van-nav-bar>
    <!-- tab标签页面 -->
    <van-tabs class="channel-tabs" v-model="active" animated swipeable>
      <!-- 列表文章组件 -->
      <van-tab v-for="item in channels" :key="item.id" :title="item.name">
        <ArticleList :channelId="item.id" />
      </van-tab>
      <!-- 右边汉堡图标 -->
      <div @click="isChanncleShow=true" slot="nav-right" class="nav-right">
        <i class="toutiao toutiao-gengduo"></i>
      </div>
      <div slot="nav-right" class="placeholder"></div>
    </van-tabs>
    <!-- 弹出层 -->
    <van-popup close-icon-position="top-left" v-model="isChanncleShow" closeable position="bottom"
      :style="{ height: '80%' }">
      <ChannleEdit @sendData="editChannelActive" :active="active" :channels="channels"></ChannleEdit>
    </van-popup>
  </div>
</template>

<script>
import { getUserChannels } from '@/api/user'
import ArticleList from '@/views/home/components/article-list.vue'
import ChannleEdit from './components/channle-edit.vue'
import { getItem } from '@/utils/storage.js'

const HMTT = 'hmtt_channel'
export default {
  name: 'home',
  data () {
    return {
      active: 0,
      channels: [],
      isChanncleShow: false
    }
  },
  methods: {
    // 获取频道列表数据
    async loadUserChannels () {
      // 判断 用户未登录 并且本地有数据
      const data = getItem(HMTT)
      if (!this.$store.state.user && getItem(HMTT)) {
        this.channels = data
      } else {
        try {
          const { data: res } = await getUserChannels()
          // console.log(res)
          this.channels = res.data.channels
        } catch (err) {
          this.$toast('获取频道列表失败')
        }
      }
    },
    // 子向父取消弹层弹出,跳转组件
    editChannelActive (e) {
      // 取消弹层
      this.isChanncleShow = e.close
      // 跳转组件
      this.active = e.index
    }

  },
  computed: {},
  watch: {},
  filters: {},
  components: {
    ArticleList,
    ChannleEdit
  },
  created () {
    this.loadUserChannels()
  },
  mounted () { }
}
</script>

<style scoped lang='less'>
.home-box {
  padding-bottom: 100px;
  padding-top: 174px;
  .search-btn {
    width: 555px;
    height: 64px;
    background: rgba(255, 255, 255, 0.2);
    border-radius: 32px;
    border: none;
  }
  /deep/.van-icon-search {
    color: #fff;
  }
  // 取消最大宽度限制
  /deep/.van-ellipsis {
    max-width: unset;
  }
  // 频道tab
  /deep/.channel-tabs {
    .van-tabs__wrap {
      position: fixed;
      top: 92px;
      z-index: 2;
      width: 100%;
    }
    .van-tab {
      height: 82px;
      min-width: 200px;
      border-right: 1px solid #edeff3;
      // 文本样式
      .van-tab__text {
        font-size: 27px;
        color: #777;
      }
    }
    // 选中样式
    .van-tab--active {
      // 文本样式
      .van-tab__text {
        font-size: 30px;
        color: #333;
      }
    }
    // 小条样式
    .van-tabs__nav {
      padding: 0;
      .van-tabs__line {
        width: 31px;
        height: 6px;
        background-color: #3296fa;
        border-radius: 3px;
        bottom: 8px;
      }
    }
    // 右边汉堡图标
    .nav-right {
      width: 66px;
      height: 82px;
      position: fixed;
      right: 0;
      background-color: rgba(255, 255, 255, 0.9);
      display: flex;
      justify-content: center;
      align-items: center;
      .toutiao {
        font-size: 33px;
      }
      &::before {
        content: '';
        width: 2px;
        height: 82px;
        background: url(~@/assets/gradient-gray-line.png);
        background-size: 100% 100%;
        position: absolute;
        left: 0;
      }
    }
    .placeholder {
      width: 66px;
      height: 82px;
      flex-shrink: 0;
    }
  }
}
</style>
